
//回到顶部
    var backTop=document.getElementById("backTop"),
    topH,//滚动条距离顶部高度
    backTopSpeed,//滚动速度
    backTopTimer;//定时器
    backTop.addEventListener('click',function(){
        var backTopTimer = setInterval(function(){
            topH = document.documentElement.scrollTop || document.body.scrollTop;
            backTopSpeed = Math.floor(-topH/8);//取整
            if(topH>0){
                document.documentElement.scrollTop = document.body.scrollTop = topH+backTopSpeed;
            }else{
                clearInterval(backTopTimer);
            }
        },30);
    });
    var backTopTimer = setInterval(function(){
        topH = document.documentElement.scrollTop || document.body.scrollTop;
        backTopSpeed = Math.floor(-topH/8);//取整
        if(topH>2){
            gotop.style.display="block";
        }else{
            gotop.style.display="none";
        }
    })
/////加载购物车
var tb = document.getElementById("tb")
$(document).ready(function() {
    var dele = document.getElementById("dele")
    var name_2 = document.querySelector(".name")
    console.log(name_2)
    var id = name_2.innerHTML;
    console.log(id)
    $(document).ready(function () {
        $.ajax({
            type: 'post',
            data: {
                pageNow: 1,
                pageSize: 100,
                userId: id,
            },
            url: "/cart/queryUserCart",
            success: function (data) {
                console.log(data);
                console.log(data.userCart.list.length)
                for(var i=0;i<data.userCart.list.length;i++) {
                    tb.innerHTML += `                    
                                   
                <ul>
                        <li style="display: none;">${data.userCart.list[i].goodsId}</li>
                        <li>
                        <div id="choice"><input type="checkbox" ></div>
                        </li>
                        <li>
                            <div id="good">
                                <a>${data.userCart.list[i].goodsName}</a>
                                
                            </div>
                        </li>
                        <li><p id="kind">${data.userCart.list[i].shopName}</p> </li>
                        <li><p id="pay">${data.userCart.list[i].goodsPrice}</p></li>
                        <li>
                            <div class="add">
                                <p id="sub" onclick="sub()">-</p>
                                <div class="char"><input type="text" value="1" id="numb"></div>
                                <p id="add" onclick="add()">+</p>
                            </div>
                        </li>
                        <li><p id="amount">${data.userCart.list[i].goodsPrice}</p></li>
                        <li> <div id="moveout" ><input id="remove" type="button" value="移出购物车"></div></li>
                </ul>
                `
                }
            },
            error: function () {
                console.log(data);
            }
        })
    })

})
//////清空购物车
    dele.onclick = function () {
        var name_2 = document.querySelector(".name")
        console.log(name_2)
        var id = name_2.innerHTML;
        console.log(id)
                var mymessage=confirm("是否确定清空购物车");
                if(mymessage==true)
                {
                    $.ajax({
                        type: 'post',
                        data: {
                            userId: id,
                        },
                        url: "/cart/deleteAllGoodsCart",
                        success: function (data) {
                            console.log(data);
                            alert("购物车已清空")
                            window.location.href="/User/myCarPage"
                        },
                        error: function () {
                            console.log(data);
                        }
                    })
                }
                else if(mymessage==false)
                {
                    return false;
                }
    }
/////移出购物车
var tb = document.getElementById("tb")
var remove = document.getElementById("remove")
$('#tb').on('click','#remove',function() {
    var good = this.parentNode.parentNode.parentNode.children[0].innerHTML;
    console.log(good)
    var name_2 = document.querySelector(".name")
    console.log(name_2)
    var id = name_2.innerHTML;
    console.log(id)
    var mymessage=confirm("是否确定删除该商品");
    if(mymessage==true)
    {
        $.ajax({
            type:'post',
            data:{
                goodsId:good,
                userId:id,
            },
            url:"/cart/deleteGoodsCart",
           success:function(data){
                alert("已清除")
               window.location.href="/User/myCarPage"
              ///////重新加载购物车
               var tb = document.getElementById("tb")
               $(document).ready(function() {
                   var dele = document.getElementById("dele")
                   var name_2 = document.querySelector(".name")
                   console.log(name_2)
                   var id = name_2.innerHTML;
                   console.log(id)
                   $(document).ready(function () {
                       $.ajax({
                           type: 'post',
                           data: {
                               pageNow: 1,
                               pageSize: 100,
                               userId: id,
                           },
                           url: "/cart/queryUserCart",
                           success: function (data) {
                               console.log(data);
                               console.log(data.userCart.list.length)
                               tb.innerHTML+="";
                               for(var i=0;i<data.userCart.list.length;i++) {
                                   tb.innerHTML += `                    
                                   
                <ul>
                        <li style="display: none;">${data.userCart.list[i].goodsId}</li>
                        <li>
                        <div id="choice"><input type="checkbox" ></div>
                        </li>
                        <li>
                            <div id="good">
                                <a>${data.userCart.list[i].goodsName}</a>
                                
                            </div>
                        </li>
                        <li><p id="kind">${data.userCart.list[i].shopName}</p> </li>
                        <li><p id="pay">${data.userCart.list[i].goodsPrice}</p></li>
                        <li>
                            <div class="add">
                                <p id="sub" onclick="sub()">-</p>
                                <div class="char"><input type="text" value="1" id="numb"></div>
                                <p id="add" onclick="add()">+</p>
                            </div>
                        </li>
                        <li><p id="amount">${data.userCart.list[i].goodsPrice}*numb.value</p></li>
                        <li> <div id="moveout" ><input id="remove" type="button" value="移出购物车"></div></li>
                </ul>
                `
                               }
                           },
                           error: function () {
                               console.log(data);
                           }
                       })
                   })

               })


           },
            error:function(){
               console.log(data)
            }

        })

    }
    else if(mymessage==false)
    {
        return false;
    }


})
var top1 = document.getElementById("top1")
var top2 = document.getElementById("top2")
var top4 = document.getElementById("top4")
top1.onclick = function(){
    var fg = localStorage.getItem("key");
    console.log(fg)
    if(fg==2){
        window.location.href="/admin/login";
    }else if(fg==3){
        alert("管理员正在审核，请耐心等待！")
    }else if(fg==4){
        window.location.href="/Merchant/personShopCar"
    }else if(fg==1){
        var r=confirm("是否申请成为商家？");
        if (r==true) {
            content.style.display = "none";
            mone.style.display = "block";
        }else{
            window.location.href="/goodsDetail"
        }
    }else{
        alert("您还没有登录，请您先登录！")
        window.location.href = "/User/login";
    }

}
top2.onclick = function(){
    var fg = localStorage.getItem("key");
    console.log(fg)
    if(fg==0){
        alert("你还没有登录，请你先登录")
        window.location.href = "User/login";
    }else{
        window.location.href="/User/userInterface"
    }
}
top4.onclick = function () {
    window.location.href="/"
}